﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Data.Linq;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Web;
using HtmlAgilityPack;
using System.Linq.Expressions;

namespace WapCrawlerLib
{
    partial class Post
    {
        private WapDataClassesDataContext dc = new WapDataClassesDataContext();

        /// <summary>
        /// Lọc post theo wapCrawlerId và topicCrawlerId
        /// </summary>
        /// <param name="topicCrawlerId"></param>
        /// <returns></returns>
        public IQueryable<Post> Filter(int topicCrawlerId)
        {
            //select all PostCrawler having topicCrawlerid
            var queryPostCrawlerId = from p in dc.TopicPostCrawlers
                                     where p.topicCrawlerId == topicCrawlerId
                                     select p.PostCrawler.postCrawlerId;

            //select all Post having postCrawlerId in queryPostCrawlerId
            var queryPost = from q in dc.Posts
                            where queryPostCrawlerId.Contains(q.PostCrawler.postCrawlerId)
                            select q;

            return queryPost;
        }

        public Post Get(int postId)
        {
            return dc.Posts.Single(p => p.postId == postId);
        }
    }

    partial class TestLinq
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();
        public TestLinq Insert1(TestLinq objTestLinq)
        {
            dc.TestLinqs.InsertOnSubmit(objTestLinq);
            dc.SubmitChanges();
            return objTestLinq;
        }

        public TestLinq Insert2()
        {
            dc.TestLinqs.InsertOnSubmit(this);
            dc.SubmitChanges();
            return this;
        }

        public TestLinq Insert3(string name, int value)
        {
            TestLinq objTestLinq = new TestLinq();
            objTestLinq.name = name;
            objTestLinq.value = value;
            dc.TestLinqs.InsertOnSubmit(objTestLinq);
            dc.SubmitChanges();
            return objTestLinq;
        }

        public TestLinq Insert4(string name, int value)
        {
            this.name = name;
            this.value = value;
            dc.TestLinqs.InsertOnSubmit(this);
            dc.SubmitChanges();
            return this;
        }

        public TestLinq Get(int id)
        {
            return dc.TestLinqs.Single(p => p.id == id);
        }

        public TestLinq Update()
        {
            dc.SubmitChanges();
            return this;
        }

        //public TestLinq Update()
        //{
        //    TestLinq objTestLinq = dc.TestLinqs.Single(p => p.id == this.id);

        //    dc.SubmitChanges();
        //    return this;
        //}

    }

    partial class FileCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public bool IsExist(string url)
        //{
        //    bool result = false;
        //    var query = from p in dc.FileCrawlers
        //                where p.fileUrl == url
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        public FileCrawler Insert(int postCrawlerId, string filename, string fileUrl, bool fileStatus)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();
            FileCrawler objFileCrawler = new FileCrawler();
            //Xác định file đó đã lưu trong csdl chưa
            try
            {
                objFileCrawler = dc.FileCrawlers.Single(p => p.fileUrl == fileUrl
                                                             && p.postCrawlerId == postCrawlerId);
            }
            catch
            {
                //Nếu chưa lưu trong csdl thì thêm vào
                objFileCrawler.fileName = fileName;
                objFileCrawler.fileUrl = fileUrl;
                objFileCrawler.fileStatus = fileStatus;
                objFileCrawler.postCrawlerId = postCrawlerId;
                dc.FileCrawlers.InsertOnSubmit(objFileCrawler);
                dc.SubmitChanges();
            }
            return objFileCrawler;
        }

        //public FileCrawler Update()
        //{
        //    FileCrawler file = dc.FileCrawlers.Single(p => p.fileCrawlerId == this.fileCrawlerId);
        //    file.fileUrl = this.fileUrl;
        //    file.fileName = this.fileName;
        //    dc.SubmitChanges();
        //    return file;
        //}
    }

    partial class ImageCrawler
    {
        

        public bool IsExist(int postCrawlerId, string imgScr)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();
            bool result = false;
            var query = from p in dc.ImageCrawlers
                        where p.imageUrl == imgScr && p.postCrawlerId == postCrawlerId
                        select p;
            if (query.Count() > 0)
                result = true;
            return result;
        }

        public ImageCrawler Insert(int postCrawlerId, string imgName, string imgScr)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();
            ImageCrawler img = new ImageCrawler();
            img.imageName = imgName;
            img.imageUrl = imgScr;
            img.postCrawlerId = postCrawlerId;
            img = Insert(img);
            return img;
        }

        public ImageCrawler Insert(ImageCrawler obj)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();
            ImageCrawler img = new ImageCrawler();
            img.imageName = obj.imageName;
            img.imageUrl = obj.imageUrl;
            img.postCrawlerId = postCrawlerId;
            dc.ImageCrawlers.InsertOnSubmit(img);
            dc.SubmitChanges();
            return img;
        }

        //public ImageCrawler Update(ImageCrawler obj)
        //{
        //    ImageCrawler img = dc.ImageCrawlers.Single(p => p.imageUrl == obj.imageUrl && p.postCrawlerId == obj.postCrawlerId);
        //    img.imageUrl = obj.imgScr;
        //    dc.SubmitChanges();
        //    return img;
        //}
    }

    partial class TopicPostCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        public bool IsExist(int topicId, int postId)
        {
            bool result = false;
            var query = from p in dc.TopicPostCrawlers
                        where p.topicCrawlerId == topicId &&
                        p.postCrawlerId == postId
                        select p;
            if (query.Count() > 0)
                result = true;
            return result;
        }

        public TopicPostCrawler Get(int topicCrawlerId, int postCrawlerId)
        {
            return dc.TopicPostCrawlers.Single(p => p.topicCrawlerId == topicCrawlerId && p.postCrawlerId == postCrawlerId);
        }

        public TopicPostCrawler Insert(TopicPostCrawler tTopicPostCrawler)
        {
            try
            {
                TopicPostCrawler objTopicPostCrawler = new TopicPostCrawler();
                objTopicPostCrawler.topicCrawlerId = tTopicPostCrawler.topicCrawlerId;
                objTopicPostCrawler.postCrawlerId = tTopicPostCrawler.postCrawlerId;
                dc.TopicPostCrawlers.InsertOnSubmit(objTopicPostCrawler);
                dc.SubmitChanges();
                return objTopicPostCrawler;
            }
            catch
            {
                return null;
            }
        }
    }

    partial class TopicCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        public bool IsExist(int value)
        {
            bool result = false;
            var query = from p in dc.TopicCrawlers
                        where p.topicValue == value
                        select p;
            if (query.Count() > 0)
                result = true;
            return result;
        }

        public bool IsExist(string topicName, int wapCrawlerId)
        {
            bool result = false;
            var query = from p in dc.TopicCrawlers
                        where p.topicName == topicName && p.wapCrawlerId == wapCrawlerId
                        select p;
            if (query.Count() > 0)
                result = true;
            return result;
        }

        public TopicCrawler Get(int topicCrawlerId)
        {
            return dc.TopicCrawlers.Single(q => q.topicCrawlerId == topicCrawlerId);
        }

        public TopicCrawler Get(string topicName, int wapCrawlerId)
        {
            return dc.TopicCrawlers.Single(p => p.topicName.ToLower() == topicName.ToLower() && p.wapCrawlerId.Value == wapCrawlerId);
        }

        public TopicCrawler Insert(TopicCrawler tTopicCrawler)
        {
            try
            {
                TopicCrawler objTopicCrawler = new TopicCrawler();
                objTopicCrawler.topicName = tTopicCrawler.topicName;
                objTopicCrawler.topicUrl = tTopicCrawler.topicUrl;
                objTopicCrawler.topicValue = tTopicCrawler.topicValue;
                objTopicCrawler.wapCrawlerId = tTopicCrawler.wapCrawlerId;
                dc.TopicCrawlers.InsertOnSubmit(objTopicCrawler);
                dc.SubmitChanges();
                return objTopicCrawler;
            }
            catch
            {
                return null;
            }
        }
    }

    partial class WapCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        public WapCrawler GetWapCrawler(int id)
        {
            return dc.WapCrawlers.Single(q => q.wapCrawlerId == id);
        }

        public bool UpdateRecent(string recentUrl)
        {
            try
            {
                WapCrawler objWapCrawler = dc.WapCrawlers.Single(q => q.wapCrawlerId == this.wapCrawlerId);
                objWapCrawler.recentCrawlerUrl = recentUrl;
                dc.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(WapCrawler tWapCrawler)
        {
            try
            {
                WapCrawler objWapCrawler = dc.WapCrawlers.Single(q => q.wapCrawlerId == tWapCrawler.wapCrawlerId);
                objWapCrawler.wapSiteUrl = tWapCrawler.wapSiteUrl;
                objWapCrawler.recentCrawlerUrl = tWapCrawler.recentCrawlerUrl;
                dc.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    partial class PostCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public IQueryable<PostCrawler> Filter()
        //{
        //    var query = from p in dc.PostCrawlers where p.postContent.Contains("http://")
        //                select p;
        //    return query;
        //}

       // /// <summary>
       // /// Chỉ lọc theo wapcrawlerid
       // /// </summary>
       // /// <param name="wapCrawlerId"></param>
       // /// <returns></returns>
       // public IQueryable<PostCrawler> FilterByWapId(int wapCrawlerId)
       //{
       //     //select topics of wap
       //     var queryTopicCrawler =  from p in dc.TopicCrawlers
       //                         where p.wapCrawlerId == wapCrawlerId
       //                         select p.topicCrawlerId;

       //     //select all post
       //     var queryPostCrawler = from p in dc.TopicPostCrawlers
       //                        where queryTopicCrawler.Contains(p.topicCrawlerId)
       //                        select p.PostCrawler;
       //    return queryPostCrawler;
       //}

        /// <summary>
        /// Lọc theo wapcrawlerid và topiccrawlerid
        /// </summary>
        /// <param name="wapCrawlerId"></param>
        /// <param name="topicCrawlerId"></param>
        /// <returns></returns>
        public IQueryable<PostCrawler> FilterByTopicId(int topicCrawlerId)
        {
            //select all post
            var queryPostCrawler = from p in dc.TopicPostCrawlers
                                   where p.topicCrawlerId == topicCrawlerId
                                   select p.PostCrawler;
            return queryPostCrawler;
        }

        public PostCrawler Get(string postUrl)
        {
            return dc.PostCrawlers.Single(p => p.postUrl == postUrl);
        }

        public PostCrawler Insert(string postUrl)
        {
            try
            {
                PostCrawler objPostCrawler = new PostCrawler();
                objPostCrawler.postUrl = postUrl;
                dc.PostCrawlers.InsertOnSubmit(objPostCrawler);
                dc.SubmitChanges();
                return objPostCrawler;
            }
            catch
            {
                return null;
            }
        }

        public PostCrawler Insert(PostCrawler tPostCrawler)
        {
            try
            {
                PostCrawler objPostCrawler = new PostCrawler();
                objPostCrawler.postUrl = tPostCrawler.postUrl;
                //objPostCrawler.postTitle = tPostCrawler.postTitle;
                //objPostCrawler.postThumbnailUrl = tPostCrawler.postThumbnailUrl;
                //objPostCrawler.postHead = tPostCrawler.postHead;
                dc.PostCrawlers.InsertOnSubmit(objPostCrawler);
                dc.SubmitChanges();
                return objPostCrawler;
            }
            catch
            {
                return null;
            }
        }

        public bool Update(PostCrawler tPostCrawler)
        {
            try
            {
                //Get
                PostCrawler objPostCrawler = dc.PostCrawlers.Single(p => p.postUrl == tPostCrawler.postUrl);
                //Update
                objPostCrawler.postUrl = tPostCrawler.postUrl;
                objPostCrawler.postTitle = tPostCrawler.postTitle;
                objPostCrawler.postThumbnailUrl = tPostCrawler.postThumbnailUrl;
                objPostCrawler.postHead = tPostCrawler.postHead;
                dc.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        ///// <summary>
        ///// Tạo post mới nếu chưa tồn tại, nếu tồn tại rồi thì bỏ qua
        ///// </summary>
        ///// <param name="itemUrl"></param>
        ///// <param name="itemTitle"></param>
        //public void CreatePost(int wapCrawlerId, string itemUrl, string itemTitle)
        //{
        //    WapDataClassesDataContext dc = new WapDataClassesDataContext();
        //    //Lấy topic của bài post
        //    TopicCrawler objTopic = dc.TopicCrawlers.Single(p => p.topicValue == 1 && p.wapCrawlerId == wapCrawlerId);
        //    //Tìm xem bài post này đã tồn tại trong CSDL chưa
        //    PostCrawler objPost = new PostCrawler();
        //    try
        //    {
        //        objPost = dc.PostCrawlers.Single(p => p.postUrl == itemUrl);
        //    }
        //    catch
        //    {
        //        objPost.postUrl = itemUrl;
        //        objPost.postTitle = itemTitle;
        //        dc.PostCrawlers.InsertOnSubmit(objPost);
        //        dc.SubmitChanges();
        //    }

        //    //Sau khi tạo mới Post xong tiếp tục tạo liên kết với Topic
        //    TopicPostCrawler objTopicPost = new TopicPostCrawler();
        //    try
        //    {
        //        objTopicPost = dc.TopicPostCrawlers.Single(p => p.topicCrawlerId == objTopic.topicCrawlerId &&
        //                                                        p.postCrawlerId == objPost.postCrawlerId);
        //    }
        //    catch
        //    {
        //        objTopicPost.topicCrawlerId = objTopic.topicCrawlerId;
        //        objTopicPost.postCrawlerId = objPost.postCrawlerId;
        //        dc.TopicPostCrawlers.InsertOnSubmit(objTopicPost);
        //        dc.SubmitChanges();
        //    }
        //}
    }
}
